#include "experimentresult.h"

ExperimentResult::ExperimentResult(Experiment * ownerExperiment)
{
    this->experiment = ownerExperiment;
    timeStamp = QDateTime::currentDateTime();
}

ExperimentResult::~ExperimentResult()
{
//    //iterate over each action result in the vector and delete
//    for(int i = 0; i < ActionResults.count(); i++)
//    {
//        delete ActionResults.at(i);
//        //just in case although it doesn't matter
//        ActionResults.remove(i);
//    }
}

void ExperimentResult::AddAction(Action *action)
{
    actionResults.push_back(action);
}

QByteArray ExperimentResult::ToXmlFile()
{
    //create xml document to write to
    QDomDocument xmlDocument;

    //create xml declaration - don't care about errors in parsing
    //QString xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
    //xmlDocument.setContent(&xmlDeclaration, NULL, NULL, NULL);

    //create root element
    QDomElement xmlRoot;

    //set experiment result attributes
    xmlRoot.setAttribute("ExperimentId", experimentId);
    xmlRoot.setAttribute("ServerId", 10);
    xmlRoot.setAttribute("TimeStamp", timeStamp.toString(Qt::ISODate));

    //set the root tag name
    xmlRoot.setTagName("ExperimentResults");

    //iterate over all actions in the vector and get the xml representation
    for(int i = 0; i < actionResults.count(); i++)
        xmlRoot.appendChild(actionResults[i]->ToResultXml());

    //append the root node to the document
    xmlDocument.appendChild(xmlRoot);

    //return a byte array represenation of the document
    return xmlDocument.toByteArray(0);
}

